#include "incognitoblockingclient.C"
#include "blog.C"

#include <fstream>

int main_create(ptr<IncognitoBlockingClient> &client) {
        warnx << "blogpublisher: main_create \n";
	bigint id=Blog::createNewBlog(client);

	warnx << "New blog created. ID= " << id << "\n";
	return 0;

}

int main_info(Blog &blog) {
	warnx << "blogpublisher: main_info \n";
	warnx << "Rootnode ID: " << blog.getRootID() << "\n"
		<< "Blog title: >" << blog.getTitle() << "<\n";
	
	return 0;
}

int do_settitle(Blog &blog, str newtitle) {
        warnx << "blogpublisher: do_settitle\n" <<"New title: " << newtitle <<" \n";

	blog.setTitle(newtitle);
	blog.saveRoot();

	return 0;
}
int do_addartfile(Blog &blog, str arttitle, str artfile) {
	warnx << "Not implemented\n";
	return 0;
}
int do_addart(Blog &blog, str arttitle, str artcont) {
	bigint artid=blog.addArticle(arttitle,artcont);
	blog.saveRoot();
	warnx << "New artile added. ID= " << artid << "\n";
	return 0;
}

int do_getart(Blog &blog, str artnum) {
	str title, content;
	unsigned int anum=atoi(artnum.cstr());
	blog.getArticle(anum,title,content);

	warnx << "Article number: " << anum << "\n";
	warnx << "Article title: >" << title << "<\n";
	warnx << "Content:\n" << content << "\n";
	
	return 0;
}
int main(int argc, char *argv[])
{
	chord_node cnode;
	
	srandom(time(0));
	if (argc < 4) {
		warn << "Usage: " << argv[0] << " host port command [prameters]\n"
			<< "Possible commands:\n"
			<< "\tcreate - create new blog\n"
			<< "\tinfo - get info about blog (param: blogid)\n"
			<< "\tsettitle - set blog title (param: blogid newtitle)\n"
			<< "\taddart - add article (param: blogid arttitle artcontent)\n"
			<< "\taddartfile - add article (param: blogid arttitle artfile)\n"
			<< "\tgetart - get article (param: blogid, artnum)\n"
			<< "\n";
		return 0;
	}
	if (inet_addr (argv[1]) == INADDR_NONE)
	{
		struct hostent *h = gethostbyname (argv[1]);
		if (!h)
			fatal << "Invalid address or hostname: " << argv[1] << "\n";
		struct in_addr *ptr = (struct in_addr *) h->h_addr;
		cnode.r.hostname = inet_ntoa (*ptr);
	} else {
		cnode.r.hostname = argv[1];
	}

	cnode.r.port = atoi (argv[2]);
	cnode.x = make_chordID (cnode.r.hostname, cnode.r.port);
	cnode.vnode_num = 0;

	ptr<IncognitoBlockingClient> client=New refcounted<IncognitoBlockingClient>(cnode);
	warn <<"blogpublisher: new ibc created\n";
	str p0=argv[3];
	warn << "Blogpublisher: argv[3] = "<< argv[3] <<"\n";
	if (p0=="create")
	{
		warn << "Blogpublisher: Invoking main_create...\n";
		return main_create(client);
	}
	bigint id;
	warnx << "not create \n";

	str2chordID(argv[4], id);
	warnx << "id = " << id << "\n";

	Blog blog(client,id);
	warnx << "Blog found id = " << id << "\n";
	if (p0=="info")
		return main_info(blog);
	else if (p0=="settitle")
		return do_settitle(blog,argv[5]);
	else if (p0=="addart")
		return do_addart(blog,argv[5],argv[6]);
	else if (p0=="addartfile")
		return do_addartfile(blog,argv[5],argv[6]);
	else if (p0=="getart")
		return do_getart(blog,argv[5]);
	warnx << "Unknown command: " << p0 << "\n";
//        amain ();

	return 0;
}
